/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain an copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.picker;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.multimodalinput.event.KeyEvent;

import com.xuexiang.xui_lib.component.button.SuperButton;
import com.xuexiang.xui_lib.component.picker.widget.BasePickerView;
import com.xuexiang.xui_lib.component.picker.widget.OptionsPickerView;
import com.xuexiang.xui_lib.component.picker.widget.builder.OptionsPickerBuilder;
import com.xuexiang.xui_lib.component.picker.widget.listener.OnDismissListener;
import com.xuexiang.xui_lib.util.StatusBarUtils;
import com.xuexiang.xuidemo.ResourceTable;

/**
 * 条件选择器--自定义选择条件
 *
 * @author ljc
 * @since 2021-04-26
 */
public class OptionsPickerAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Image imageBack;
    private Text textTitle;
    private Text textTitleBottom;
    private SuperButton btnSexPicker;
    private SuperButton btnClassPicker;
    private SuperButton btnConstellationPicker;
    private SuperButton btnNationPicker;
    private Component mComponent;
    private String[] mSexOption;
    private int sexSelectOption = 0;

    private String[] mGradeOption;
    private String[] mClassOption;
    private String[] mConstellationOption;
    private String[] mNationOption;
    private int gradeSelectOption = 0;
    private int classSelectOption = 0;
    private int constellationSelectOption = 0;
    private int nativeSelectOption = 0;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        StatusBarUtils.translucent();
        StatusBarUtils.fullScreen();
        super.setUIContent(ResourceTable.Layout_ability_options_picker);
        initView();
    }

    private void initView() {
        findComponentById();
        setClick();
        mSexOption = getDatamSexOption();
        mGradeOption = getDatammGradeOption();
        mConstellationOption = getDatammConstellationOption();
        mNationOption = getDatammmNationOption();
        mClassOption = new String[30];
        for (int i = 0; i < mClassOption.length; i++) {
            mClassOption[i] = (i + 1) + "班";
        }
        textTitle.setText("OptionsPickerView");
        textTitleBottom.setText("条件选择器--自定义选择条件");
    }

    private void findComponentById() {
        imageBack = (Image) findComponentById(ResourceTable.Id_image_back);
        textTitle = (Text) findComponentById(ResourceTable.Id_text_title);
        textTitleBottom = (Text) findComponentById(ResourceTable.Id_text_title_bottom);

        btnSexPicker = (SuperButton) findComponentById(ResourceTable.Id_btn_sex_picker);
        btnClassPicker = (SuperButton) findComponentById(ResourceTable.Id_btn_class_picker);
        btnConstellationPicker = (SuperButton) findComponentById(ResourceTable.Id_btn_constellation_picker);
        btnNationPicker = (SuperButton) findComponentById(ResourceTable.Id_btn_nation_picker);
        mComponent = (Component) findComponentById(ResourceTable.Id_component);
    }

    private void setClick() {
        imageBack.setClickedListener(this);
        btnSexPicker.setClickedListener(this);
        btnClassPicker.setClickedListener(this);
        btnConstellationPicker.setClickedListener(this);
        btnNationPicker.setClickedListener(this);
        mComponent.setClickedListener(this);
    }

    /**
     * 点击事件
     *
     * @param component
     */
    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_image_back:
                onBackPressed();
                break;
            /**
             * 性别选择器
             */
            case ResourceTable.Id_btn_sex_picker:
                showSexPickerView();
                break;
            /**
             * 年级选择器
             */
            case ResourceTable.Id_btn_class_picker:
                showClassPickerView();
                break;
            /**
             * 星座选择器
             */
            case ResourceTable.Id_btn_constellation_picker:
                showConstellationPickerView();
                break;
            /**
             * 民族选择器
             */
            case ResourceTable.Id_btn_nation_picker:
                showNativePickerView();
                break;
            default:
                break;
        }
    }

    /**
     * 获取性别数据
     *
     * @return 字符串数组
     */
    private String[] getDatamSexOption() {
        String[] tags_values = new String[0];
        try {
            tags_values = getContext().getResourceManager().getElement(ResourceTable.Strarray_sex_option).getStringArray();
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
        return tags_values;
    }

    ;

    /**
     * 获取班级字符数组数据
     *
     * @return String[]
     */
    private String[] getDatammGradeOption() {
        String[] tags_values = new String[0];
        try {
            tags_values = getContext().getResourceManager().getElement(ResourceTable.Strarray_grade_option).getStringArray();
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
        return tags_values;
    }

    ;

    /**
     * 获取星座数据
     *
     * @return String[]
     */
    private String[] getDatammConstellationOption() {
        String[] tags_values = new String[0];
        try {
            tags_values = getContext().getResourceManager().getElement(ResourceTable.Strarray_constellation_entry).getStringArray();
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
        return tags_values;
    }

    ;

    /**
     * 获取民族数据
     *
     * @return String[]
     */
    private String[] getDatammmNationOption() {
        String[] tags_values = new String[0];
        try {
            tags_values = getContext().getResourceManager().getElement(ResourceTable.Strarray_nation_value).getStringArray();
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
        return tags_values;
    }

    ;

    /**
     * 性别选择
     */
    private void showSexPickerView() {
        mComponent.setVisibility(Component.VISIBLE);
        OptionsPickerView pvOptions = new OptionsPickerBuilder(getContext(), (v, options1, options2, options3) -> {
            btnSexPicker.setText(mSexOption[options1]);
            sexSelectOption = options1;
            return false;
        })
            .setTitleText("性别选择")
            .setSelectOptions(sexSelectOption)
            .setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(BasePickerView pickerView) {
                    mComponent.setVisibility(Component.HIDE);
                }
            })
            .build();
        pvOptions.setPicker(mSexOption);
        pvOptions.show();
    }


    /**
     * 班级选择
     */
    private void showClassPickerView() {
        mComponent.setVisibility(Component.VISIBLE);
        OptionsPickerView pvOptions = new OptionsPickerBuilder(getContext(), (v, options1, options2, options3) -> {
            btnClassPicker.setText(String.format("%s%s", mGradeOption[options1], mClassOption[options2]));
            gradeSelectOption = options1;
            classSelectOption = options2;
            return false;
        })
            .setTitleText("班级选择")
            .setSelectOptions(gradeSelectOption, classSelectOption)
            .setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(BasePickerView pickerView) {
                    mComponent.setVisibility(Component.HIDE);
                }
            })
            .build();
        pvOptions.setNPicker(mGradeOption, mClassOption);
        pvOptions.show();
    }

    /**
     * 星座选择
     */
    private void showConstellationPickerView() {
        mComponent.setVisibility(Component.VISIBLE);
        OptionsPickerView pvOptions = new OptionsPickerBuilder(getContext(), (v, options1, options2, options3) -> {
            btnConstellationPicker.setText(mConstellationOption[options1]);
            constellationSelectOption = options1;
            return false;
        })
            .setTitleText("星座选择")
            .setSelectOptions(constellationSelectOption)
            .setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(BasePickerView pickerView) {
                    mComponent.setVisibility(Component.HIDE);
                }
            })
            .build();
        pvOptions.setPicker(mConstellationOption);
        pvOptions.show();
    }

    /**
     * 民族选择
     */
    private void showNativePickerView() {
        mComponent.setVisibility(Component.VISIBLE);
        OptionsPickerView pvOptions = new OptionsPickerBuilder(getContext(), (v, options1, options2, options3) -> {
            btnNationPicker.setText(mNationOption[options1]);
            nativeSelectOption = options1;
            return false;
        })
            .setTitleText("民族选择")
            .setSelectOptions(nativeSelectOption)
            .setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(BasePickerView pickerView) {
                    mComponent.setVisibility(Component.HIDE);
                }
            })
            .build();
        pvOptions.setPicker(mNationOption);
        pvOptions.show();
    }

    /**
     * 监听返回键
     *
     * @param keyCode
     * @param keyEvent
     * @return boolean
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {
        mComponent.setVisibility(Component.HIDE);
        return super.onKeyUp(keyCode, keyEvent);
    }
}
